package com.yangzb.sample.retrofit;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.thread.ThreadUtil;
import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.BaseGlobalInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.interceptor.NetworkInterceptor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import top.chaser.framework.common.base.util.JSONUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

@Component
@Slf4j
public class Runner implements ApplicationRunner {

    @Autowired
    GitHubService service;

    @Override
    public void run(ApplicationArguments args) throws IOException, InterruptedException, ExecutionException {
        ExecutorService executorService = ThreadUtil.newExecutor(10, 10, 100000);
        List<CallTest> tasks = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            CallTest thread = new CallTest(i,service);
            tasks.add(thread);
        }
        StopWatch stopWatch = StopWatch.create("call");
        stopWatch.start();
        List<Future<Integer>> futures = executorService.invokeAll(tasks);
        stopWatch.stop();

        System.out.println(stopWatch.prettyPrint());
        System.out.println(stopWatch.getTotalTimeSeconds());

        for (Future<Integer> future : futures) {
            Integer integer = future.get();
        }
        executorService.shutdown();
    }
    @RetrofitClient(baseUrl = "${url}",poolName = "test1")
    public interface GitHubService {
        @GET("users/lianjiatech")
            Call<Object> listRepos(@Query("t")Integer i);
    }
public static class CallTest implements Callable<Integer> {
    private Integer i ;
    GitHubService service;

    public CallTest(Integer i,GitHubService service){
        this.i = i;
        this.service = service;
    }
    @Override
    public Integer call() throws Exception {
        Response<Object> execute = service.listRepos(i).execute();
        System.out.println(execute.body());
        return i;
    }
}
}

